package apollo.game.core;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Input.Keys;

public class Input implements InputProcessor {

	public static final int UP = 3;
	public static final int DOWN = 0;
	public static final int LEFT = 6;
	public static final int RIGHT = 9;
	public static final int GAME_A = 1;
	public static final int GAME_B = 2;
	public static final int GAME_C = 4;

	public boolean[] buttons = new boolean[10];
	public boolean[] prevButtons = new boolean[10];
	
	private int keyCode = -1;
	
	public void set(int key, boolean down) {
        if (key == Keys.KEYCODE_DPAD_UP) keyCode = UP;
        if (key == Keys.KEYCODE_DPAD_LEFT) keyCode = LEFT;
        if (key == Keys.KEYCODE_DPAD_DOWN) keyCode = DOWN;
        if (key == Keys.KEYCODE_DPAD_RIGHT) keyCode = RIGHT;
        
        if (keyCode >= 0) {
            buttons[keyCode] = down;
        }
        if (down == false) {
        	keyCode = -1;
        }
	}
	
	public int getKeyCode() {
		return keyCode;
	}

	public void tick() {
		for (int i = 0; i < buttons.length; i++) {
			prevButtons[i] = buttons[i];
		}

		if (Gdx.app.getType() == ApplicationType.Android) {
			boolean left = false;
			boolean right = false;
			boolean z = false;
			boolean s = false;

			for (int i = 0; i < 2; i++) {
				int x = (int) ((Gdx.input.getX(i) / (float) Gdx.graphics
						.getWidth()) * 320);
				if (!Gdx.input.isTouched(i))
					continue;
				if (x < 32) {
					set(Keys.KEYCODE_DPAD_LEFT, true);
					left |= true;
				}
				if (x > 32 && x < 90) {
					set(Keys.KEYCODE_DPAD_RIGHT, true);
					right |= true;
				}
				if (x > 320 - 64 && x < 320 - 32) {
					set(Keys.KEYCODE_Z, true);
					z |= true;
				}
				if (x > 320 - 32 && x < 320) {
					set(Keys.KEYCODE_X, true);
					s |= true;
				}
			}

		   	 if(left==false) set(Keys.KEYCODE_DPAD_LEFT, false);
		   	 if(right==false) set(Keys.KEYCODE_DPAD_RIGHT, false);
		   	 if(z==false) set(Keys.KEYCODE_Z, false);
		   	 if(s==false) set(Keys.KEYCODE_X, false);
		}
	}
	
	public void releaseAllKeys() {
		for (int i = 0; i < buttons.length; i++) {
			if (i == UP || i == DOWN)
				continue;
			buttons[i] = false;
		}
	}

	@Override
	public boolean keyDown(int keycode) {
		set(keycode, true);
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		set(keycode, false);
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int x, int y, int pointer, int button) {
		return false;
	}

	@Override
	public boolean touchUp(int x, int y, int pointer, int button) {
		x = (int) (x / (float) Gdx.graphics.getWidth() * 320);
		if (x > 160 - 32 && x < 160) {
			set(Keys.KEYCODE_DPAD_UP, !buttons[UP]);
			if (buttons[UP])
				buttons[DOWN] = false;
		}
		if (x > 160 && x < 160 + 32) {
			set(Keys.KEYCODE_DPAD_DOWN, !buttons[DOWN]);
			if (buttons[DOWN])
				buttons[UP] = false;
		}
		System.out.println("buttons: " + buttons[UP] + ", " + buttons[DOWN]);
		return false;
	}

	@Override
	public boolean touchDragged(int x, int y, int pointer) {
		return false;
	}

	@Override
	public boolean touchMoved(int x, int y) {
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		return false;
	}

}
